class MidwayProvider {

    static ConstructorAdapter() {
        if (window.Midway === undefined) {
            return function (target) {
            }
        }
        return function (target) {
        }
    }

    static MethodAdapter() {
        if (window.Midway === undefined) {
            return function (target, key, descriptor) {
                return descriptor;
            }
        }
        return function (target, key, descriptor) {
            let method = descriptor.value;
            if (key === 'requireComponent') {
                descriptor.value = function (...args) {
                    const options = requireAdapter.call(this, args);
                    return method.call(this, options);
                };
            } else if (key === 'addEventListener') {
                descriptor.value = function (...args) {
                    const name = arguments[0], type = arguments[1], callback = arguments[2];
                    let eventType = type;
                    if(type === ConstVariable.PresenterEventType.IC_PLAYER_EVENT) {
                        eventType = 'web_event';
                    }
                    if (callback === null || typeof callback !== 'function') {
                        throw Error('callback is null or not a function');
                    }
                    this.PlayerController.getEventBus().addEventListener(name, eventType, callback, this);
                };
            } else if (key === 'removeEventListener') {

            } else if (key === 'dispatchEvent') {
                descriptor.value = function (...args) {
                    const name = args[0], type = args[1], data = args[2] || {};
                    let eventType = type, eventScope;
                    if(type === ConstVariable.PresenterEventType.IC_PLAYER_EVENT) {
                        eventType = 'web_event';
                    }
                    if(eventType === 'web_event') {
                        eventScope = args[3] || 'current_bundle';
                    }
                    this.PlayerController.getEventBus().dispatchEvent(name, eventType, data, eventScope);
                };
            } else if (key === "_init") {
                descriptor.value = function () {
                    this._environment.isApp = (window.Midway.Settings.Runtime === 'mobile');
                    this._environment.isWeb = (window.Midway.Settings.Runtime === 'web');
                    this._environment.isPC = (window.Midway.Settings.Runtime === 'pc');
                    this._environment.isTeacher = (window.Midway.Terminal === 'teacher');
                    this._environment.isStudent = (window.Midway.Terminal === 'student');
                    method.call(this);
                };
            }
            descriptor.writable = descriptor.enumerable = descriptor.configurable = false;
            return descriptor;
        }
    }
}

function requireAdapter(...args) {
    const name = args[0][0], props = args[0][1], rendTo = args[0][2], layout = args[0][3];
    let model = {};
    if (Array.prototype.isArray(props) && props.length > 0) {
        props.forEach(item=> {
            model[item.name] = item.value || (item.text ? JSON.parse(item.text) : undefined);
        });
    }
    let element = undefined;
    if(rendTo !== undefined) {
        if(typeof rendTo === 'string') {
            element = this.View.querySelector(rendTo);
        } else if(rendTo instanceof jQuery) {
            element = rendTo[0];
        } else {
            element = rendTo;
        }
    }
    const extendLayout = $.extend(layout, {style: {width: '100%', height: '100%'}});
    return {
        code: name,
        model: model,
        attributes: extendLayout,
        rendTo: element
    }
}

export {MidwayProvider}